﻿using AutoMapper;
using Enterprise_Set.API.Application.Commands.Week1.Warehouse_CRUD;
using MediatR;
using Mysqlx.Crud;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week1.Warehouse_CRUD
{
    /// <summary>
    /// 添加仓库
    /// </summary>
    public class AddWarehouseHandler : IRequestHandler<AddWarehouseCommand, Result>
    {
        public AddWarehouseHandler(IBaseRepository<Warehouse> warehouseRepository, IMapper mapper)
        {
            WarehouseRepository = warehouseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 仓库
        /// </summary>
        public IBaseRepository<Warehouse> WarehouseRepository { get; set; }
        public IMapper mapper { get; set; }
        public async Task<Result> Handle(AddWarehouseCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result() { Code = (int)CommonErrorCodes.Added };
                var AddData = mapper.Map<Warehouse>(request);
                string sql = "insert into Warehouse values(@WarehouseId,@WarehousNumBer,@WarehouseName,@WarehouseAddr,@WarehouseRemark,@WarehouseState,@WarehouseIsDel,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
                var AddNum = WarehouseRepository.Add(sql, new
                {
                    WarehouseId = AddData.WarehouseId,
                    WarehousNumBer = AddData.WarehousNumBer,
                    WarehouseName = AddData.WarehouseName,
                    WarehouseAddr = AddData.WarehouseAddr,
                    WarehouseRemark = AddData.WarehouseRemark,
                    WarehouseState = AddData.WarehouseState,
                    WarehouseIsDel = AddData.WarehouseIsDel,
                    Handle = AddData.Handle,
                    HandleContext = AddData.HandleContext,
                    HandleTime = DateTime.Now,
                    Pid = AddData.Pid,
                    IsDelete = AddData.IsDelete
                });
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
